package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    //注入checkItemDao对象
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组,针对t_checkGroup表
        checkGroupDao.add(checkGroup);

        //设置检查组和检查项关联,针对t_checkgroup_checkitem表
        //通过mybatis框架提供的selectKey标签来获得自增产生的id值
        Integer checkgroupId = checkGroup.getId();

        if (checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                //设置存储容器,存入checkgroupId,checkitemId
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //分页助手插件
        PageHelper.startPage(currentPage,pageSize);
        //调用dao的条件查询
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        //total和数据集合都封装在page对象中
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckitemIdsByCheckgroupId(Integer checkgroupId) {
        return checkGroupDao.findCheckitemIdsByCheckgroupId(checkgroupId);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //编辑检查组的信息
        checkGroupDao.edit(checkGroup);
        //编辑检查组之前,先清理原先关联的检查项
        checkGroupDao.deleteAssocication(checkGroup.getId());
        //重新建立关联
        if (checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                //设置存储容器,存入checkgroupId,checkitemId
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkGroup.getId());
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public void delete(int id) {
        //删除t_checkgroup_checkitem关系表中的数据
        checkGroupDao.deleteAssocication(id);
        //删除t_checkgroup检查组中的数据
        checkGroupDao.delete(id);
    }
}
